<?php
class Users extends MY_Controller {
    public function __construct()
	{
        parent::__construct();
		$this->load->model('user_model');
		$this->load->library('HybridAuthLib');
		$member = $this->session->userdata('user');
		$this->settingdata=$this->general_model->select_val("setting","*","id = 1");
		
		$this->is_login=$member['logged_in'];
		$this->memberid=$member['userid'];
		$this->usertype=$member['groupid'];
		$this->session_id = $this->session->userdata('session_id');
		ini_set('memory_limit', '96M');
		ini_set('upload_max_filesize', '96M');
		ini_set('post_max_size', '96M');
    }
	public function send_mail($to,$subject,$message)
	{
		$settingdata=$this->general_model->select_val("setting","*","id = 1");
		/*$config=Array(
			"protocol"=>"smtp",
			"smtp_host"=>"ssl://smtp.gmail.com",
			"smtp_port"=>465,
			"smtp_user"=>"niranjannsahoo@gmail.com",
			"smtp_pass"=>"paikarapursibu"
		);
		$this->load->library('email',$config);*/
		$this->load->library('email');
		$this->email->set_mailtype("html");
		$this->email->set_newline("\r\n");
		$this->email->from($settingdata['notification_email'],$settingdata['site_name']);
		$this->email->reply_to($settingdata['notification_email'], $settingdata['site_name']);	
		$this->email->to($to);		
		$this->email->subject($subject);		
		$this->email->message($message);
		$this->email->send();
	}
	public function sendmail($to,$from,$subject,$message,$optional=array())
	{
		$fromname='';
		$toname='';
		if(!empty($optional))
		{
			$fromname=isset($optional['fromname'])?$optional['fromname']:'';
			$toname=isset($optional['toname'])?$optional['toname']:'';
		}
		
		/*$config=Array(
			"protocol"=>"smtp",
			"smtp_host"=>"ssl://smtp.gmail.com",
			"smtp_port"=>465,
			"smtp_user"=>"niranjannsahoo@gmail.com",
			"smtp_pass"=>"paikarapursibu"
		);
		$this->load->library('email',$config);*/
		$this->load->library('email');
		$this->email->set_mailtype("html");
		$this->email->set_newline("\r\n");
		$this->email->from($from,$fromname);
		$this->email->reply_to($from, $fromname);	
		$this->email->to($to);		
		$this->email->subject($subject);		
		$this->email->message($message);
		$this->email->send();
	}
	
	public function pagination($totalrows,$baseurl,$perpage,$offset='')
	{
		$this->load->library('pagination');
		$data['query_string'] = ( ! empty($_SERVER['QUERY_STRING'])) ? '?' . $_SERVER['QUERY_STRING'] : $_GET;
		// Create Pagination
		//echo $baseurl;
		$config['base_url'] = $baseurl;
		//$config['page_query_string'] = TRUE;
		$config['total_rows'] = $totalrows;
		$config['per_page'] = $perpage;
		$config['uri_segment'] = '3';
		$config['num_links'] = '4' ;
		$config['cur_page'] = 0;
		//$config['use_page_numbers'] = TRUE;
		//$config['query_string_segment'] ="page";
		//$config['suffix'] = '?'.http_build_query($_GET, '', "&");
		 //$config['suffix'] = '?per_page='.$config['per_page']; 
		$config['suffix'] = $data['query_string'];
		$config['first_url'] = $config['base_url'].$config['suffix'];
		//echo $config['first_url'];
		$this->pagination->initialize($config); 
		$pagination['bottom'] = $this->pagination->create_links();
		$start = $this->pagination->cur_page * $this->pagination->per_page - ($this->pagination->per_page-1);
		$end = $start + $this->pagination->per_page -1;
		if($end > $totalrows)
		{
			$end=$totalrows;
		}
		$total = $this->pagination->total_rows;
		$pagination['top']="Results: $start - $end of about $total for ";
		return $pagination;
	}
	public function verify($userid,$rand)
	{
		$check=$this->general_model->count_row("users","userid = '".$userid."' and activation_code = '".$rand."' and activated = 0");
		if($check==1)
		{
			$updatedata=array("activated"=>1);
			$this->general_model->update("users",$updatedata,"userid = ".$userid);
			$this->session->set_flashdata('message', '<p class="success">Thanks for Confirmation! Your account activated,You can login your account.</p>');
		}
		else
		{
			$this->session->set_flashdata('message', '<p class="success">Confirmation Error! We are sorry but your login name/registration stamp is wrong or you have already confirmed! .</p>');
		}
		redirect(BASE_URL);
	}
	public function changepassword()
	{
		if($this->general_model->loggedin("user") == FALSE)
		{
			redirect(BASE_URL);
		}
		$userid=$this->memberid;
		if($this->input->server('REQUEST_METHOD') === 'POST')
		{
			$userdata=array("password"=>md5($this->input->post('password')),
							"show_password"=>$this->input->post('password'));
			$this->general_model->update('users',$userdata,"userid = ".$userid);
			$this->session->set_flashdata('message', '<p class="success">Password Changed successfully</p>');
			redirect(BASE_URL.'users/changepassword');
		}
		$data['page_title']="Privacy Setting";
		$data['alanmember']=$this->general_model->get_join2table("alan_member","users","*,alan_member.country,alan_member.state,alan_member.city,","alan_member.userid=users.userid","alan_member.userid = ".$this->memberid);
		$this->template->content->view('front/changepassword',$data);
		$this->template->publish();
	}
	public function index()
	{
		if($this->general_model->loggedin("user") == FALSE) 
		{
	  		$this->session->set_userdata("redirect","users");	
			redirect('users/login');
    	}
		$this->activemenu="My Account";
		$data['memberdetails']=$this->general_model->select_val("speedhost_member","*","userid = ".$this->memberid);
		$data['usertype']=$this->usertype;
		$data['register_domain']=$this->general_model->count_row("speedhost_domains","userid =".$this->memberid);
		$nextmonthdate=date("Y-m-d",strtotime("+1 month",strtotime(date("Y-m-d"))));
		
		$data['expiring_domain']=$this->general_model->count_row("speedhost_domains","(exp_date between ".date('Y-m-d')." and ".$nextmonthdate.") and userid =".$this->memberid);
		$data['expired_domain']=$this->general_model->count_row("speedhost_domains","exp_date < ".date('Y-m-d')." and userid =".$this->memberid);
		$data['hosted_domain']=$this->general_model->count_row("speedhost_domains","webhosted = 1 and userid =".$this->memberid);
		
		$this->template->content->view('front/myaccount',$data);
		$this->template->publish();
	}
	public function login()
	{
		if($this->input->server('REQUEST_METHOD') === 'POST')
		{
			$query = $this->user_model->validateuser();
			if($query) // if the user's credentials validated...
			{
				$this->session->unset_userdata('redirect');
				$member = $this->session->userdata('user');
				//redirect(BASE_URL.'users');
				if ($this->input->post('redirect'))
				{
					redirect(BASE_URL.$this->input->post('redirect'));
				} 
				else
				{
					redirect(BASE_URL.'users');
				}
			}
			else 
			{
				$this->session->set_flashdata('message', 'That email/password combination does not exist');
				if ($this->input->post('redirect'))
				{
					$this->session->set_userdata("redirect",$this->input->post('redirect'));
				}
				
				redirect(BASE_URL.'users/login');
			}
		}
		if ($this->input->post('redirect')) {
			$data['redirect'] = $this->input->post('redirect');
		} else if ($this->session->userdata('redirect')) {
			//echo $this->session->userdata('redirect'); 
      		$data['redirect'] = $this->session->userdata('redirect');
	  		//$this->session->unset_userdata('redirect');
			
					  	
    	} else {
			$data['redirect'] = '';
		}
		$this->template->content->view('front/login',$data);
		$this->template->publish();
	}
	public function logout()
	{
		$this->user_model->logout();
		redirect(BASE_URL);
	}
	public function register()
	{
		$data['response']="";
		if($this->input->server('REQUEST_METHOD') === 'POST')
		{
			$url="&command=CreateSubAccount";
			$url.="&newuid=".$this->input->post('newuid');
			$url.="&newpw=".$this->input->post('newpw');
			$url.="&confirmpw=".$this->input->post('confirmpw');
			$url.="&RegistrantOrganizationName=".$this->input->post('RegistrantOrganizationName');
			$url.="&RegistrantFirstName=".$this->input->post('RegistrantFirstName');
			$url.="&RegistrantLastName=".$this->input->post('RegistrantLastName');
			$url.="&RegistrantJobTitle=".$this->input->post('RegistrantJobTitle');
			$url.="&RegistrantAddress1=".$this->input->post('RegistrantAddress1');
			$url.="&RegistrantAddress2=".$this->input->post('RegistrantAddress2');
			$url.="&RegistrantCity=".$this->input->post('RegistrantCity');
			if($this->input->post('RegistrantStateProvinceChoice')=="S")
			$url.="&RegistrantStateProvince=".$this->input->post('RegistrantState');
			else
			$url.="&RegistrantStateProvince=".$this->input->post('RegistrantProvince');
			$url.="&RegistrantStateProvinceChoice=".$this->input->post('RegistrantStateProvinceChoice');
			$url.="&RegistrantPostalCode=".$this->input->post('RegistrantPostalCode');
			$url.="&RegistrantCountry=".$this->input->post('RegistrantCountry');
			$url.="&RegistrantEmailAddress=".$this->input->post('RegistrantEmailAddress');
			$url.="&RegistrantEmailAddress_Contact=".$this->input->post('RegistrantEmailAddress_Contact');
			$url.="&RegistrantPhone=+".$this->input->post('RegistrantPhonecode').".".$this->input->post('RegistrantPhone');
			$url.="&RegistrantFax=+".$this->input->post('RegistrantFaxcode').".".$this->input->post('RegistrantFax');
			$url.="&AuthQuestionType=".$this->input->post('AuthQuestionType');
			$url.="&AuthQuestionAnswer=".$this->input->post('AuthQuestionAnswer');
			
			//echo $url;
			$response=enom($url);
			if(isset($response['error']))
			{
				$data['response']=$response['error'];
			}
			else
			{
				//$data['response']= $api['interface-response']['NewAccount']['StatusCustomerInfo'];
				/*user information*/
				$userdata=array(
						"groupid"=>3,
						"group_type"=>'USER',
						"firstname"=>$this->input->post('RegistrantFirstName'),
						"lastname"=>$this->input->post('RegistrantLastName'),
						"username"=>$this->input->post('newuid'),
						"email"=>$this->input->post('RegistrantEmailAddress'),
						"password"=>md5($this->input->post('newpw')),
						"show_password"=>$this->input->post('newpw'),
						"phoneno"=>$this->input->post('RegistrantPhone'),
						"address"=>$this->input->post('RegistrantAddress1'),
						"address2"=>$this->input->post('RegistrantAddress2'),
						"city"=>$this->input->post('RegistrantCity'),
						"state_id"=>$this->input->post('state_id'),
						"state"=>$this->input->post('RegistrantState'),
						"country_id"=>$this->input->post('country_id'),
						"country"=>$this->input->post('RegistrantCountry'),
						"zip"=>$this->input->post('RegistrantPostalCode'),
						"activated"=>1,
						"activation_code"=>"retailer",
						"approve"=>1,
						"date_added"=>date('Y-m-d H:i:s'),
						);
				$userid=$this->general_model->insert("users",$userdata);
				/*speedhost member information*/
				$memberdata=array(
						"userid"=>$userid,
						"firstname"=>$this->input->post('RegistrantFirstName'),
						"lastname"=>$this->input->post('RegistrantLastName'),
						"login_id"=>$this->input->post('newuid'),
						"email"=>$this->input->post('RegistrantEmailAddress'),
						"organisation_name"=>$this->input->post('RegistrantOrganizationName'),
						"jobtitle"=>$this->input->post('RegistrantJobTitle'),
						"address1"=>$this->input->post('RegistrantAddress1'),
						"address2"=>$this->input->post('RegistrantAddress2'),
						"city"=>$this->input->post('RegistrantCity'),
						"rspchoice"=>$this->input->post('RegistrantStateProvinceChoice'),
						"state"=>$this->input->post('RegistrantState'),
						"province"=>$this->input->post('RegistrantProvince'),
						"country"=>$this->input->post('RegistrantCountry'),
						"zip"=>$this->input->post('RegistrantPostalCode'),
						"account_email"=>$this->input->post('RegistrantEmailAddress_Contact'),
						"phone_code"=>$this->input->post('RegistrantPhonecode'),
						"phone"=>$this->input->post('RegistrantPhone'),
						"fax_code"=>$this->input->post('RegistrantFaxcode'),
						"fax"=>$this->input->post('RegistrantFax'),
						"secret_question"=>$this->input->post('AuthQuestionType'),
						"secret_answer"=>$this->input->post('AuthQuestionAnswer'),
						"date_added"=>date('Y-m-d H:i:s'),
						);
				$memberid=$this->general_model->insert("speedhost_member",$memberdata);
				
				$this->session->set_flashdata('message', 'Successfully account created');
				redirect(BASE_URL.'users/register');
			}
			
			
		}	
		if (isset($_POST['RegistrantOrganizationName'])) {
			$data['RegistrantOrganizationName'] = $_POST['RegistrantOrganizationName'];
		}else {
			$data['RegistrantOrganizationName'] = '';
		}
		
		if (isset($_POST['RegistrantFirstName'])) {
			$data['RegistrantFirstName'] = $_POST['RegistrantFirstName'];
		}else {
			$data['RegistrantFirstName'] = '';
		}
		
		if (isset($_POST['RegistrantLastName'])) {
			$data['RegistrantLastName'] = $_POST['RegistrantLastName'];
		}else {
			$data['RegistrantLastName'] = '';
		}
		
		if (isset($_POST['RegistrantJobTitle'])) {
			$data['RegistrantJobTitle'] = $_POST['RegistrantJobTitle'];
		}else {
			$data['RegistrantJobTitle'] = '';
		}
		
		if (isset($_POST['RegistrantAddress1'])) {
			$data['RegistrantAddress1'] = $_POST['RegistrantAddress1'];
		}else {
			$data['RegistrantAddress1'] = '';
		}
		
		if (isset($_POST['RegistrantAddress2'])) {
			$data['RegistrantAddress2'] = $_POST['RegistrantAddress2'];
		}else {
			$data['RegistrantAddress2'] = '';
		}
		
		if (isset($_POST['RegistrantCity'])) {
			$data['RegistrantCity'] = $_POST['RegistrantCity'];
		}else {
			$data['RegistrantCity'] = '';
		}
		
		if (isset($_POST['RegistrantStateProvinceChoice'])) {
			$data['RegistrantStateProvinceChoice'] = $_POST['RegistrantStateProvinceChoice'];
		}else {
			$data['RegistrantStateProvinceChoice'] = '';
		}
		
		if (isset($_POST['RegistrantState'])) {
			$data['RegistrantState'] = $_POST['RegistrantState'];
		}else {
			$data['RegistrantState'] = '';
		}
		
		if (isset($_POST['RegistrantProvince'])) {
			$data['RegistrantProvince'] = $_POST['RegistrantProvince'];
		}else {
			$data['RegistrantProvince'] = '';
		}
		
		if (isset($_POST['RegistrantPostalCode'])) {
			$data['RegistrantPostalCode'] = $_POST['RegistrantPostalCode'];
		}else {
			$data['RegistrantPostalCode'] = '';
		}
		
		if (isset($_POST['RegistrantCountry'])) {
			$data['RegistrantCountry'] = $_POST['RegistrantCountry'];
		}else {
			$data['RegistrantCountry'] = '';
		}
		
		if (isset($_POST['RegistrantEmailAddress'])) {
			$data['RegistrantEmailAddress'] = $_POST['RegistrantEmailAddress'];
		}else {
			$data['RegistrantEmailAddress'] = '';
		}
		
		if (isset($_POST['RegistrantEmailAddress_Contact'])) {
			$data['RegistrantEmailAddress_Contact'] = $_POST['RegistrantEmailAddress_Contact'];
		}else {
			$data['RegistrantEmailAddress_Contact'] = '';
		}
		
		if (isset($_POST['RegistrantPhonecode'])) {
			$data['RegistrantPhonecode'] = $_POST['RegistrantPhonecode'];
		}else {
			$data['RegistrantPhonecode'] = '';
		}
		
		if (isset($_POST['RegistrantPhone'])) {
			$data['RegistrantPhone'] = $_POST['RegistrantPhone'];
		}else {
			$data['RegistrantPhone'] = '';
		}
		
		if (isset($_POST['RegistrantFaxcode'])) {
			$data['RegistrantFaxcode'] = $_POST['RegistrantFaxcode'];
		}else {
			$data['RegistrantFaxcode'] = '';
		}
		
		if (isset($_POST['RegistrantFax'])) {
			$data['RegistrantFax'] = $_POST['RegistrantFax'];
		}else {
			$data['RegistrantFax'] = '';
		}
		
		if (isset($_POST['newuid'])) {
			$data['newuid'] = $_POST['newuid'];
		}else {
			$data['newuid'] = '';
		}
		
		if (isset($_POST['AuthQuestionType'])) {
			$data['AuthQuestionType'] = $_POST['AuthQuestionType'];
		}else {
			$data['AuthQuestionType'] = '';
		}
		
		if (isset($_POST['AuthQuestionAnswer'])) {
			$data['AuthQuestionAnswer'] = $_POST['AuthQuestionAnswer'];
		}else {
			$data['AuthQuestionAnswer'] = '';
		}
		
	
		$data['countries']=$this->general_model->select("country");
		$data['us_states']=$this->general_model->select("zone","*","country_id = 223");
		$this->template->content->view('front/register',$data);
		$this->template->publish();
	}
	public function domainsearch()
	{
		$this->activemenu="Domains";
		$this->template->content->view('front/domainsearch');
		$this->template->publish();
	}
	public function search()
	{
		//$data['response']="";
		$data['searchresult']=array();
		//$data['relativedomain']=array();
		if($this->input->server('REQUEST_METHOD') === 'GET')
		{
			$url="&command=check";
			//$search=explode('.',$_GET['search']);
			if(strpos($_GET['search'], '.')!="")
			{
				$tld=substr($_GET['search'],strpos($_GET['search'], '.')+1);
				$sld=substr($_GET['search'],0,strpos($_GET['search'], '.'));
			}
			else
			{
				$tld='com';
				$sld=$_GET['search'];
			}
			
			$url.="&sld=".$sld."&tld=".$tld;
			$data['searchresult']=enom($url);
			//set price
			$price=$this->general_model->select_val("speedhost_tld_config","*","tld ='".$tld."'");
			if(isset($data['searchresult']['api']))
			{
				$data['searchresult']['api']['price']=$price['register_price'];
			}
			$url1="&command=check&sld=".$sld."&tldlist=net,org,ninja,co,social,xyz,supplies,club";
			$url2="&command=NameSpinner&sld=".$sld."&tld=".$tld."&MaxResults=6&Similar=High";
			$url3="&command=check&sld=".$sld;
			
			$data['searchresult']['relativedomainurl']=$url1;
			$data['searchresult']['suggestedurl']=$url2;
			$data['searchresult']['moredomainurl']=$url3;
			
		}
		/*echo "<pre>";
		print_r($data); */
		$this->template->content->view('front/search',$data);
		$this->template->publish();
	}
	public function cart()
	{
		/*if($this->general_model->loggedin("user") == FALSE) 
		{
	  		$this->session->set_userdata("redirect","users/cart");	
			redirect('users/login');
			
    	}*/
		if($this->general_model->loggedin("user") == TRUE) 
		{
	  		$storecart=$this->general_model->select_val('speedhost_member','cart','userid = '.$this->memberid);
			$cart=unserialize($storecart['cart']);
    	}
		else
		{
			$cart=$this->cart->contents();
		}
		$data['cart']=$this->get_nested($cart);
		
		$data['cartcontent']=$this->load->view('front/cartcontent',$data,true);
		//echo $data['cartcontent'];
		$this->template->content->view('front/cart',$data);
		$this->template->publish();
	}
	public function checkout()
	{
		if($this->general_model->loggedin("user") == FALSE) 
		{
	  		$this->session->set_userdata("redirect","users/checkout");	
			redirect('users/login');
    	}
		$data['memberdetails']=$this->general_model->select_val("speedhost_member","*","userid = ".$this->memberid);
		$data['response']="";
		/*if($this->input->server('REQUEST_METHOD') === 'POST')
		{
			
			$cart=$this->cart->contents();
			$cartarray=$this->get_nested($cart);
			
			foreach($cartarray as $item)
			{
				$count=0;
				$ChargeAmount=0;
				$ChargeAmount+=$item['subtotal'];
				if ( isset($item['child']))
				{
					foreach($item['child'] as $childitem)
					{
						$ChargeAmount+=$childitem['subtotal'];
					}
				}
				$url="&SLD=".$item['sld'];
				$url.="&TLD=".$item['tld'];
				$url.="&command=Purchase";
				$url.="&EndUserIP=".$_SERVER['REMOTE_ADDR'];
				/*$url.="&NumYears=".$item['qty'];
				$url.="&ChargeAmount=".$ChargeAmount;
				$url.="&UseDNS=default";
				$url.="&UseCreditCard=Yes";
				$url.="&CardType=".$this->input->post('CardType');
				$url.="&CCName=".$this->input->post('CCName');
				$url.="&CreditCardNumber=".$this->input->post('CreditCardNumber');
				$url.="&CreditCardExpMonth=".$this->input->post('CreditCardExpMonth');
				$url.="&CreditCardExpYear=".$this->input->post('CreditCardExpYear');
				$url.="&CVV2=".$this->input->post('CVV2');
				$url.="&CCAddress=".$this->input->post('CCAddress');
				$url.="&CCCity=".$this->input->post('CCCity');
				if($this->input->post('CCCountry')=="United States")
				$url.="&CCStateProvince=".$this->input->post('CCState');
				else
				$url.="&CCStateProvince=".$this->input->post('CCProvince');
				$url.="&CCZip=".$this->input->post('CCZip');
				$url.="&CCPhone=+".$this->input->post('CountryCode').".".$this->input->post('PhoneNumber');
				$url.="&CCCountry=".$this->input->post('CCCountry');
				*/
			
				/*$url.="&RegistrantOrganizationName=".$data['memberdetails']['organisation_name'];
				$url.="&RegistrantFirstName=".$data['memberdetails']['firstname'];
				$url.="&RegistrantLastName=".$data['memberdetails']['lastname'];
				$url.="&RegistrantJobTitle=".$data['memberdetails']['jobtitle'];
				$url.="&RegistrantAddress1=".$data['memberdetails']['address1'];
				$url.="&RegistrantAddress2=".$data['memberdetails']['address2'];
				$url.="&RegistrantCity=".$data['memberdetails']['city'];
				if($data['memberdetails']['rspchoice']=="S")
				$url.="&RegistrantStateProvince=".$data['memberdetails']['state'];
				else
				$url.="&RegistrantStateProvince=".$data['memberdetails']['province'];
				$url.="&RegistrantStateProvinceChoice=".$data['memberdetails']['rspchoice'];
				$url.="&RegistrantPostalCode=".$data['memberdetails']['zip'];
				$url.="&RegistrantCountry=".$data['memberdetails']['country'];
				$url.="&RegistrantEmailAddress=".$data['memberdetails']['account_email'];
				$url.="&RegistrantPhone=+".$data['memberdetails']['phone_code'].".".$data['memberdetails']['phone'];
				$url.="&RegistrantFax=+".$data['memberdetails']['fax_code'].".".$data['memberdetails']['fax'];
				$response=enom($url);
				if(empty($response))
				{
					break;
				}
				else
				{
					if(isset($response['error']))
					{
						$data['response']=$response['error'];
					}
					else
					{
						$data['response']="Successfully Created";
					}
					continue;
				}
				
				
			}

		}*/
		$carts=$this->cart->contents();
		$data['cart']=$this->get_nested($carts);
		if (isset($_POST['CardType'])) {
			$data['CardType'] = $_POST['CardType'];
		}else {
			$data['CardType'] = '';
		}
		
		if (isset($_POST['CCName'])) {
			$data['CCName'] = $_POST['CCName'];
		}else {
			$data['CCName'] = '';
		}
		
		if (isset($_POST['CreditCardNumber'])) {
			$data['CreditCardNumber'] = $_POST['CreditCardNumber'];
		}else {
			$data['CreditCardNumber'] = '';
		}
		
		if (isset($_POST['CreditCardNumber'])) {
			$data['CreditCardNumber'] = $_POST['CreditCardNumber'];
		}else {
			$data['CreditCardNumber'] = '';
		}
		
		if (isset($_POST['CreditCardExpMonth'])) {
			$data['CreditCardExpMonth'] = $_POST['CreditCardExpMonth'];
		}else {
			$data['CreditCardExpMonth'] = '';
		}
		
		if (isset($_POST['CreditCardExpYear'])) {
			$data['CreditCardExpYear'] = $_POST['CreditCardExpYear'];
		}else {
			$data['CreditCardExpYear'] = '';
		}
		
		if (isset($_POST['CVV2'])) {
			$data['CVV2'] = $_POST['CVV2'];
		}else {
			$data['CVV2'] = '';
		}
		
		if (isset($_POST['CCAddress'])) {
			$data['CCAddress'] = $_POST['CCAddress'];
		}elseif($data['memberdetails']['address1']!='') {
			$data['CCAddress'] = $data['memberdetails']['address1'];
		}else{
			$data['CCAddress']='';
		}
		
		if (isset($_POST['CCCity'])) {
			$data['CCCity'] = $_POST['CCCity'];
		}elseif($data['memberdetails']['city']!='') {
			$data['CCCity'] = $data['memberdetails']['city'];
		}else{
			$data['CCCity']='';
		}
		
		if (isset($_POST['CCCountry'])) {
			$data['CCCountry'] = $_POST['CCCountry'];
		}elseif($data['memberdetails']['country']!='') {
			$data['CCCountry'] = $data['memberdetails']['country'];
		}else{
			$data['CCCountry']='';
		}
		
		if (isset($_POST['CCState'])) {
			$data['CCState'] = $_POST['CCState'];
		}elseif($data['memberdetails']['state']!='') {
			$data['CCState'] = $data['memberdetails']['state'];
		}else{
			$data['CCState']='';
		}
		
		if (isset($_POST['CCProvince'])) {
			$data['CCProvince'] = $_POST['CCProvince'];
		}elseif($data['memberdetails']['province']!='') {
			$data['CCProvince'] = $data['memberdetails']['province'];
		}else{
			$data['CCProvince']='';
		}
		
		if (isset($_POST['CCZip'])) {
			$data['CCZip'] = $_POST['CCZip'];
		}elseif($data['memberdetails']['zip']!='') {
			$data['CCZip'] = $data['memberdetails']['zip'];
		}else{
			$data['CCZip']='';
		}
		
		if (isset($_POST['CountryCode'])) {
			$data['CountryCode'] = $_POST['CountryCode'];
		}elseif($data['memberdetails']['phone_code']!='') {
			$data['CountryCode'] = $data['memberdetails']['phone_code'];
		}else{
			$data['CountryCode']='';
		}
		
		if (isset($_POST['PhoneNumber'])) {
			$data['PhoneNumber'] = $_POST['PhoneNumber'];
		}elseif($data['memberdetails']['phone']!='') {
			$data['PhoneNumber'] = $data['memberdetails']['phone'];
		}else{
			$data['PhoneNumber']='';
		}
		
		
		$data['countries']=$this->general_model->select("country");
		$data['us_states']=$this->general_model->select("zone","*","country_id = 223");
		
		$this->template->content->view('front/checkout',$data);
		$this->template->publish();
	}
	public function domain_manage()
	{
		$tab=$this->uri->segment(3);
		if($this->general_model->loggedin("user") == FALSE) 
		{
	  		$this->session->set_userdata("redirect","users/domain_manage/$tab");	
			redirect('users/login');
    	}
		
		if($tab=="register")
		{
			$data['register_domain']=$this->general_model->get_jointable("speedhost_domains","speedhost_tld_config","*","speedhost_domains.tld=speedhost_tld_config.tld","speedhost_domains.userid =".$this->memberid);
			
			$this->template->content->view('front/mydomain',$data);
		}
		elseif($tab=="dns")
		{
			$data['register_domain']=$this->general_model->get_jointable("speedhost_domains","speedhost_tld_config","*","speedhost_domains.tld=speedhost_tld_config.tld","speedhost_domains.userid =".$this->memberid);
			$this->template->content->view('front/mydomain',$data);
		}
		elseif($tab=="expired")
		{
			$data['expired_domain']=$this->general_model->get_jointable("speedhost_domains","speedhost_tld_config","*","speedhost_domains.tld=speedhost_tld_config.tld","speedhost_domains.exp_date < '".date('Y-m-d')."' and speedhost_domains.userid =".$this->memberid);
			
			$this->template->content->view('front/expiredomain',$data);
		}
		else
		{
			$data['register_domain']=$this->general_model->get_jointable("speedhost_domains","speedhost_tld_config","*","speedhost_domains.tld=speedhost_tld_config.tld","speedhost_domains.userid =".$this->memberid);
			
			$this->template->content->view('front/mydomain',$data);
		}
		$this->template->publish();
	}
	public function newtld()
	{
		$this->activemenu="New TLDs";
		$data['all_preregister_tld']=$this->general_model->select("speedhost_newtld","*","QName = 'Pre-registration'");
		$this->template->content->view('front/newtld',$data);
		$this->template->publish();
	}
	public function preregister()
	{
		$this->activemenu="Pre Register";
		$data['all_preregister_tld']=$this->general_model->select("speedhost_newtld","*","QName = 'Pre-registration'");
		$this->template->content->view('front/preregister',$data);
		$this->template->publish();
	}
	public function landrush()
	{
		$this->activemenu="Priority Placement";
		$data['all_landrush_tld']=$this->general_model->select("speedhost_newtld","*","QName = 'Landrush'");
		$this->template->content->view('front/landrush',$data);
		$this->template->publish();
	}
	public function sunrise()
	{
		$this->activemenu="Trademark Holders";
		$data['all_sunrise_tld']=$this->general_model->select("speedhost_newtld","*","QName = 'Sunrise'");
		$this->template->content->view('front/sunrise',$data);
		$this->template->publish();
	}
	public function watchlist()
	{
		$this->activemenu="Build a Watchlist";
		$data['all_category']=$this->general_model->select("speedhost_tld_category");
		$data['all_category_tld']=$this->general_model->select("speedhost_newtld","*","tldstatus_desc = 'General Availability' and QStatusID = '2'");
		
		$this->template->content->view('front/watchlist',$data);
		$this->template->publish();
	}
	public function mywatchlist()
	{
		if($this->general_model->loggedin("user") == FALSE) 
		{
	  		$this->session->set_userdata("redirect","users/mywatchlist");	
			redirect('users/login');
    	}
		$this->activemenu="My Watchlist";
		$data['all_watchlist']=$this->general_model->select("speedhost_watchlist","*","userid = ".$this->memberid);
		
		$this->template->content->view('front/mywatchlist',$data);
		$this->template->publish();
	}
	public function tlddetails($tld)
	{
		$data['tlddetails']=$this->general_model->select("speedhost_newtld","*","tld_name = '$tld'");
		$this->template->content->view('front/tlddetails',$data);
		$this->template->publish();
	}
	public function domaintransfer()
	{
		if($this->general_model->loggedin("user") == FALSE) 
		{
	  		$this->session->set_userdata("redirect","users/domaintransfer");	
			redirect('users/login');
    	}
		
		$this->template->content->view('front/domaintransfer');
		//$this->template->content->view('front/domaintransfer_contact');
		//$this->template->content->view('front/domaintransferorder');
		//$this->template->content->view('front/domaintransfersuccess');
		$this->template->publish();
	}
	public function premium_search()
	{
		if($_GET)
		{
			$url="&Command=NM_Search";
			$url.="&Keyword=".$_GET['search'];
			$url.="&SortBy=quality&RecordsToReturn=10&StartPosition=1";
			$response=enom($url);
			if(isset($response['error']))
			{
				$data['error']=$response['error'];
			}
			else
			{
				$data['result']=$response['api'];
			}
			$this->template->content->view('front/premiumsearch',$data);
		
		}
		else
		{
			$this->template->content->view('front/premium_search');
		}
		$this->template->publish();
	}
	
	public function whois_search()
	{
		if($_GET)
		{
			$url="&command=getwhoiscontact";
			if(strpos($_GET['search'], '.')!="")
			{
				$tld=substr($_GET['search'],strpos($_GET['search'], '.')+1);
				$sld=substr($_GET['search'],0,strpos($_GET['search'], '.'));
			}
			else
			{
				$tld='com';
				$sld=$_GET['search'];
			}
			
			$url.="&sld=".$sld."&tld=".$tld;
			$response=enom($url);
			//echo "<pre>";
			//print_r($response);
			if(isset($response['error']))
			{
				$data['error']=$response['error'];
				$this->template->content->view('front/whois_search',$data);
			}
			else
			{
				$data['response']=$response['api'];
				$this->template->content->view('front/whois_search_result',$data);
			}
		}
		else
		{
			$this->template->content->view('front/whois_search');
		}
		$this->template->publish();
	}
	public function backorder_domain()
	{
		if($_GET)
		{
			$url="&command=AdvancedDomainSearch";
			if(strpos($_GET['search'], '.')!="")
			{
				$tld=substr($_GET['search'],strpos($_GET['search'], '.')+1);
				$sld=substr($_GET['search'],0,strpos($_GET['search'], '.'));
			}
			else
			{
				$tld='com';
				$sld=$_GET['search'];
			}
			$url.="&SearchCriteria=Start";
			$url.="&sld=".$sld;
			$url.="&OrderBy=ExpDate&RegistrationStatus=Expired";
			$response=enom($url);
			
			//echo "<pre>";
			//print_r($response);
			if(isset($response['error']))
			{
				$data['error']=$response['error'];
				$this->template->content->view('front/backorder_domain',$data);
			}
			else
			{
				$data['response']=$response['api'];
				$this->template->content->view('front/backorder_domain_result',$data);
			}
		}
		else
		{
			$this->template->content->view('front/backorder_domain');
		}
		
		$this->template->publish();
	}
	public function tld_category()
	{
		$this->activemenu="Domain Extension (TLDs)";
		$data['all_category']=$this->general_model->select("speedhost_tld_category");
		$data['all_category_tld']=$this->general_model->select("speedhost_newtld","*","tldstatus_desc = 'General Availability' and QStatusID = '2'");
		
		$this->template->content->view('front/tld_category',$data);
		$this->template->publish();
	}	
	public function bulktools()
	{
		
		$this->template->content->view('front/bulktools');
		$this->template->publish();
	}
	public function bulkregister()
	{	
		if($this->general_model->loggedin("user") == FALSE) 
		{
	  		$this->session->set_userdata("redirect","users/bulkregister");	
			redirect('users/login');
    	}
		
		$this->template->content->view('front/bulkregister');
		$this->template->publish();
	}
	public function bulkrenew()
	{	
		if($this->general_model->loggedin("user") == FALSE) 
		{
	  		$this->session->set_userdata("redirect","users/bulkrenew");	
			redirect('users/login');
    	}
		
		$this->template->content->view('front/bulkrenew');
		$this->template->publish();
	}
	
	public function hosting()
	{
		$this->template->content->view('front/hosting');
		$this->template->publish();
	}
	public function linuxhosting()
	{
		$packagename=$this->uri->segment(3);
		$data['alllinuxpackage']=$this->general_model->select("speedhost_hostingpackage","*","ostype = 'Linux'");
			
		if($packagename)
		{
			$data['packagename']=$packagename;
			$this->template->content->view('front/purchasehosting',$data);
			$this->template->publish();
		}
		else
		{
			$this->template->content->view('front/linuxhosting',$data);
			$this->template->publish();
		}
	}
	
	public function mailtest()
	{
		$to="niranjan@diggity.us";
		$from="niranjannsahoo@gmail";
		$subject="Test Mail";
		$message="Hi This Mail Worked";
		$this->send_mail($to,$subject,$message);
			
	}
	
	
	
	
	
	/*for testing*/
	public function testxml()
	{
		//print_r($this->settingdata['notification_email']);
		//$xml= "https://resellertest.enom.com/interface.asp?uid=resellid&pw=resellpw&command=check&sld=dfdfd&tld=com&tldlist=net,org,ninja,co,social,parts&responsetype=xml";
		//$xml="https://resellertest.enom.com/interface.asp?command=NameSpinner&UID=resellid&PW=resellpw&SLD=google&TLD=com&MaxResults=8&Similar=High&ResponseType=XML";
		//$xml="https://resellertest.enom.com/interface.asp?UID=ResellID&PW=resellpw&ResponseType=xml&Command=Queue_GetInfo"; 
		//$xml="https://resellertest.enom.com/interface.asp?command=TLD_GetCategory&UID=ResellID&PW=resellpw&ResponseType=XML "; 
		//$xml="https://resellertest.enom.com/interface.asp?command=getextattributes&uid=resellid&pw=resellpw&tld=info&responsetype=xml";
		//$xml="https://resellertest.enom.com/interface.asp?command=AdvancedDomainSearch&uid=resellid&pw=resellpw&SearchCriteria=Start&SLD=r&OrderBy=ExpDate&RegistrationStatus=Expired&responsetype=XML";
		//$xml="https://resellertest.enom.com/interface.asp?command=addbulkdomains&uid=resellid&pw=resellpw&producttype=register&listcount=1&sld1=resellerdocs1&tld1=com&responsetype=xml";
		$xml="https://resellertest.enom.com/interface.asp?command=WebHostGetResellerPackages&uid=resellid&pw=resellpw&responsetype=xml"; 
		
		if ($xmldata=@simplexml_load_file($xml))
		{
			
			$arrylist=xmlToArray($xmldata);
			echo "<pre>";
			print_r($arrylist);
			
		}
		
		else 
		{
			echo 'Error!';
		}
		
	}
	public function updatehostpackage()
	{
		$xml="https://resellertest.enom.com/interface.asp?command=WebHostGetResellerPackages&uid=resellid&pw=resellpw&responsetype=xml"; 
		if ($xmldata=@simplexml_load_file($xml))
		{
			
			$arrylist=xmlToArray($xmldata);
			$i=1;
			foreach($arrylist['interface-response']['HostPackages']['HostPackage'] as $HostPackage)
			{
				$packagedata=array(
					'package_id'=>$HostPackage['PackageID'],
					'packagename'=>is_array($HostPackage['PackageName']) ? '' : $HostPackage['PackageName'],
					'bandwidth'=>$HostPackage['BandwidthGB'],
					'webstorage'=>$HostPackage['WebStorageMB'],
					'database_type'=>$HostPackage['PackageID'],
					'database_storage'=>$HostPackage['DatabaseType'],
					'popmailbox'=>$HostPackage['POPMailBoxes'],
					'ostype_id'=>$HostPackage['OSTypeID'],
					'ostype'=>$HostPackage['OSType'],
					'domainheader'=>$HostPackage['DomainHeaders'],
					
				);
				$this->general_model->insert("speedhost_hostingpackage",$packagedata);
				echo "Inserted row ".$i.'<br>';
				
				$i++;
			}
		}
	}
	public function updatehostpackageprice()
	{
		$xml="https://resellertest.enom.com/interface.asp?command=CalculateAllHostPackagePricing&uid=resellid&pw=resellpw&responsetype=xml"; 
		if ($xmldata=@simplexml_load_file($xml))
		{
			
			$arrylist=xmlToArray($xmldata);
			$i=1;
			foreach($arrylist['interface-response']['resellerpackagepricing']['package'] as $HostPackage)
			{
				$packagedata=array(
					'package_cost'=>$HostPackage['PackageCost'],
					'sell_price'=>$HostPackage['SellPrice'],
					
				);
				$this->general_model->update("speedhost_hostingpackage",$packagedata,"packagename ='".$HostPackage['PackageName']."' and ostype_id =".$HostPackage['OsTypeID']);
				echo "Updated row ".$i.'<br>';
				
				$i++;
			}
		}
	}
	public function sevecategory()
	{
		$xml="https://resellertest.enom.com/interface.asp?command=TLD_GetCategory&UID=ResellID&PW=resellpw&ResponseType=XML "; 
		if ($xmldata=@simplexml_load_file($xml))
		{
			
			$arrylist=xmlToArray($xmldata);
			$i=1;
			foreach($arrylist['interface-response']['TLD_Category']['Category'] as $product)
			{
				$tlddata=array(
					'category_name'=>$product,
					
				);
				$this->general_model->insert("speedhost_tld_category",$tlddata);
				echo "Inserted row ".$i.'<br>';
				
				$i++;
			}
		}
	}
	public function savenewtld()
	{
		$xml="https://resellertest.enom.com/interface.asp?UID=ResellID&PW=resellpw&ResponseType=xml&Command=Queue_GetInfo"; 
		if ($xmldata=@simplexml_load_file($xml))
		{
			
			$arrylist=xmlToArray($xmldata);
			$i=1;
			foreach($arrylist['interface-response']['Queue_GetInfo']['Queue'] as $product)
			{
				$tlddata=array(
					'tld_name'=>$product['TLD'],
					'tld_category'=>$product['CategoryName'],
					'tld_desc'=>(is_array($product['TLDDescription']) ? '' : $product['TLDDescription']),
					'tldstatus_desc'=>$product['TLDStatusDesc'],
					'QID'=>(is_array($product['QID']) ? '' : $product['QID']),
					'QName'=>(is_array($product['QName']) ? '' : $product['QName']),
					'QStatusID'=>$product['QStatusID'],
					'QStatusName'=>$product['QStatusName'],
					'tld_price'=>(is_array($product['Price']) ? '' : $product['Price']),
					'start_date'=>(is_array($product['StartDate']) ? '' : date("Y-m-d",strtotime($product['StartDate']))),
					'end_date'=>(is_array($product['EndDate']) ? '' : date("Y-m-d",strtotime($product['EndDate']))),
				);
				$this->general_model->insert("speedhost_newtld",$tlddata);
				echo "Inserted row ".$i.'<br>';
				
				$i++;
			}
		}
	}
	public function saveprice()
	{
		//print_r($this->settingdata['notification_email']);
		$xml= "https://resellertest.enom.com/interface.asp?command=PE_GETDOMAINPRICING&uid=triplenetworkadmin&pw=P_12345678&responsetype=xml";
		
		if ($xmldata=@simplexml_load_file($xml))
		{
			
			$arrylist=xmlToArray($xmldata);
			$i=1;
			foreach($arrylist['interface-response']['pricestructure']['product'] as $product)
			{
				$tlddata=array(
					'tld'=>$product['tld'],
					'tldid'=>$product['tldid'],
					'register_price'=>$product['registerprice'],
					'transfer_price'=>$product['transferprice'],
					'renew_price'=>$product['renewprice'],
					'min_years'=>1,
					'max_years'=>10
				);
				$this->general_model->insert("speedhost_tld_config",$tlddata);
				echo "Inserted row ".$i;
				$i++;
			}
			/*echo "<pre>";
			print_r($arrylist);*/
			
		}
		
		else 
		{
			echo 'Error!';
		}
		
	}
	public function updateprice()
	{
		$ccode=array();
		$tldlist=array("com","net","org","biz","ws","info","cc","tv","bz","nu","us");
		$alldomainlist=$this->general_model->select("speedhost_tld_config");
		$countrycode=$this->general_model->select("country");
		foreach($countrycode as $code)
		{
			$ccode[]=strtolower($code['iso_code_2']);
		}
		
		$i=1;
		foreach($alldomainlist as $domain)
		{
			$tld=explode('.',$domain['tld']);
			/*if(in_array($tld[0],$ccode))
			{
				$updatedata=array('international_tld'=>1);
				$this->general_model->update("speedhost_tld_config",$updatedata,"id = ".$domain['id']);
			}
			if(isset($tld[1]))
			{
				if(in_array($tld[1],$ccode))
				{
					$updatedata=array('international_tld'=>1);
					$this->general_model->update("speedhost_tld_config",$updatedata,"id = ".$domain['id']);
				}
			}*/
			
			if(!in_array($tld[0],$tldlist))
			{
				//echo "exist ".$i.'<br>';
				$updatedata=array('new_tld'=>1);
				$this->general_model->update("speedhost_tld_config",$updatedata,"id = ".$domain['id']);
			}
			else
			{
				$updatedata=array('new_tld'=>0);
				$this->general_model->update("speedhost_tld_config",$updatedata,"id = ".$domain['id']);
			}
			$i++;
		} 
		
			/*echo "<pre>";
			print_r($arrylist);*/
			
		
		
		
		
	}
	public function viewcart()
	{
		//$ip = getenv("REMOTE_ADDR") ; 
		//echo $ip;
		$cart=$this->cart->contents();
		$newcart=$this->get_nested($cart);
		echo "<pre>";
		print_r($newcart);
	}
	public function get_nested ($treelist)
	{
		
		$array = array();
		foreach ($treelist as $tree) {
			
			if (!$tree['prowid']) 
			{
				// This page has no parent
				$array[$tree['rowid']] = $tree;
			}
			else
			{
				// This is a child page
				$array[$tree['prowid']]['child'][]= $tree;
				//$this->get_nested($tree);
			}
		}
		return $array;
	}
	public function validateRegistrationForm()
	{
		$registrationrules=array(
			'email' => array(
				'field' => 'email', 
				'label' => 'Email', 
				'rules' => 'trim|required|valid_email|is_unique[users.email]'
			),
		);
		$this->form_validation->set_rules($registrationrules);
		$this->form_validation->set_message('is_unique','This Email already taken');
		$this->form_validation->set_error_delimiters('<label class="error">', '</label>');
		if ($this->form_validation->run() == TRUE)
		{
			return true;
    	}
		else
		{
			return false;
    	}
	}
	public function validateSettingForm()
	{
		$settingrules=array(
			'username' => array(
			'field' => 'username', 
			'label' => 'Username', 
			'rules' => 'trim|alpha_dash|is_unique[zap_member.username]'
			)
		);
		$this->form_validation->set_rules($settingrules);
		$this->form_validation->set_message('is_unique','This username already in use');
		$this->form_validation->set_message('alpha_dash','This username already in use');
		$this->form_validation->set_error_delimiters('<label class="error">', '</label>');
		if ($this->form_validation->run() == TRUE)
		{
			return true;
    	}
		else
		{
			return false;
    	}
	}
}
?>